package com.amikomgamedev.labirinhantu.entity;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.tmx.TMXLayer;

import com.amikomgamedev.labirinhantu.game.Game_Data;

public class Entity_Camera implements Game_Data
{
	private float	m_Zoom_In;
	private float	m_Zoom_Out;
	
	private ZoomCamera	m_Camera;
	private Sprite		m_Focus;
	private TMXLayer	m_Map;
	
	public Entity_Camera(ZoomCamera p_Camera
			, TMXLayer p_Map
			, Sprite p_Focus)
	{
		m_Camera	= p_Camera;
		m_Focus		= p_Focus;
		m_Map		= p_Map;
		
		float m_Ratio_Width		= RES_SPLIT_MAIN_WIDTH / p_Map.getWidth();
		float m_Ratio_Height	= RES_HEIGHT / p_Map.getHeight();
		
		m_Zoom_In	= RES_HEIGHT / (20 * 25);
		m_Zoom_Out	= m_Ratio_Height < m_Ratio_Width 
				? m_Ratio_Height
				: m_Ratio_Width;
		
		m_Camera.setZoomFactor(m_Zoom_In);
	}
	
	public void onUpdate()
	{
		float p_Center_X		= m_Focus.getX() + (m_Focus.getWidth() / 2) + RES_SPLIT_TASKBAR_WIDTH / 2 / m_Camera.getZoomFactor();
		float p_Center_Y		= m_Focus.getY() + m_Focus.getHeight() / 2;
		float p_Min_Center_X	= RES_WIDTH / 2 / m_Camera.getZoomFactor();
		float p_Max_Center_X	= m_Map.getWidth() - (RES_WIDTH / 2 - RES_SPLIT_TASKBAR_WIDTH) / m_Camera.getZoomFactor();
		float p_Min_Center_Y	= RES_HEIGHT / 2 / m_Camera.getZoomFactor();
		float p_Max_Center_Y	= m_Map.getHeight() - RES_HEIGHT / 2 / m_Camera.getZoomFactor();
		
		if (m_Map.getWidth() < RES_SPLIT_MAIN_WIDTH / m_Camera.getZoomFactor())
			p_Center_X	= (m_Map.getWidth() + RES_SPLIT_TASKBAR_WIDTH / m_Camera.getZoomFactor()) / 2;
		else if (p_Center_X < p_Min_Center_X)
			p_Center_X	= p_Min_Center_X;
		else if (p_Center_X > p_Max_Center_X)
			p_Center_X	= p_Max_Center_X;

		if (m_Map.getHeight() < RES_HEIGHT / m_Camera.getZoomFactor())
			p_Center_Y	= m_Map.getHeight() / 2;
		else if (p_Center_Y < p_Min_Center_Y)
			p_Center_Y	= p_Min_Center_Y;
		else if (p_Center_Y > p_Max_Center_Y)
			p_Center_Y	= p_Max_Center_Y;
		
		m_Camera.setCenter(
				p_Center_X
				, p_Center_Y);
	}
	
	public void zoomIn()
	{
		m_Camera.setZoomFactor(m_Zoom_In);
	}
	
	public void zoomOut()
	{
		m_Camera.setZoomFactor(m_Zoom_Out);
	}
	
	public boolean isZoomIn()
	{
		return m_Camera.getZoomFactor() == m_Zoom_In ? true : false;
	}
}
